#include <REGX52.H>
#include "Delay.h"

unsigned char MatrixKey()
{
    unsigned char KeyNumber;
    P1=0x7F;
    if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=1;}
    if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=2;}
    if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=3;}
    if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=4;}

    P1=0xBF;
    if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=5;}
    if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=6;}
    if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=7;}
    if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=8;}
    
    P1=0xDF;
    if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=9;}
    if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=10;}
    if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=11;}
    if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=12;}
    
    P1=0xEF;
    if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=13;}
    if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=14;}
    if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=15;}
    if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=16;}
    
    return KeyNumber;

}
